//
//  SummaryCell.m
//  CarCare
//
//  Created by ileo on 15/4/13.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "SummaryCell.h"
#import "LineChartView.h"

#define CELLHEIGHT 200
#define GAP 10

@interface SummaryCell()

@property (nonatomic, strong) UILabel *titleL;
@property (nonatomic, strong) UILabel *avgInfoL;
@property (nonatomic, strong) UILabel *sumNumL;
@property (nonatomic, strong) UILabel *sumTitleL;

@property (nonatomic, strong) UILabel *noDataTipsL;

@property (nonatomic, strong) LineChartView *lineChartV;

@end

@implementation SummaryCell

+(CGFloat)cellHeight{
    return CELLHEIGHT;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.backgroundColor = [UIColor clearColor];
        
        UIView *bgV = [[[UIView viewWithFrame:CGRectMake(GAP, GAP, __MAIN_WIDTH - 2 * GAP, CELLHEIGHT - GAP - 5)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self];
        
        bgV.layer.cornerRadius = 4;
        bgV.layer.borderColor = COLOR_HEXSTRING(@"#c8c8c8").CGColor;
        bgV.layer.borderWidth = 0.5;
        
        self.titleL = [Factory labelWithFrame:CGRectMake(GAP, GAP, 100, 15) font:FONT_DEFAULT_Light(15) text:nil textColor:[Theme wordColor] onView:bgV textAlignment:NSTextAlignmentLeft];
        
        self.avgInfoL = [Factory labelWithFrame:CGRectMake(GAP, self.titleL.bottom, 250, 15) font:FONT_DEFAULT_Light(10) text:nil textColor:COLOR_HEXSTRING(@"#868686") onView:bgV textAlignment:NSTextAlignmentLeft];
        
        self.sumNumL = [Factory labelWithFrame:CGRectMake(bgV.width - GAP - 100, GAP, 100, 15) font:FONT_DEFAULT_Light(15) text:nil textColor:COLOR_HEXSTRING(@"#ea5a28") onView:bgV textAlignment:NSTextAlignmentRight];
        self.sumTitleL = [Factory labelWithFrame:CGRectMake(bgV.width - GAP - 100, self.titleL.bottom, 100, 15) font:FONT_DEFAULT_Light(10) text:nil textColor:COLOR_HEXSTRING(@"#868686") onView:bgV textAlignment:NSTextAlignmentRight];
        
        self.lineChartV = [[LineChartView alloc] initWithFrame:CGRectMake(GAP, self.sumTitleL.bottom, bgV.width - 2 * GAP, 145)];
        [bgV addSubview:self.lineChartV];
        
        self.noDataTipsL = [Factory labelWithFrame:CGRectMake(0, 0, 150, 60) font:FONT_DEFAULT_Light(23) text:@"暂无数据" textColor:[COLOR_HEXSTRING(@"#454545") colorWithAlphaComponent:0.4] onView:self textAlignment:NSTextAlignmentCenter];
        self.noDataTipsL.center = CGPointMake(__MAIN_WIDTH/2, bgV.centerY - 3);
    }
    return self;
    
}

-(void)setModel:(SummaryModel *)model{

    _model = model;
    self.titleL.text = model.title;
    self.avgInfoL.text = model.avgInfo;
    self.sumNumL.text = model.sumNum;
    self.sumTitleL.text = model.sumTitle;
    [self.lineChartV setDatas:model.dataInfos WithAvg:model.avgNum];
    
    CGFloat gap = 0, left = 0;
    switch (model.dateType) {
        case Summary_Year:
            gap = -13;
            left = 15;
            break;
        case Summary_Month:
            gap = -22;
            left = 0;
            break;
        case Summary_Week:
            gap = -5;
            left = 0;
            break;
  
        default:
            break;
    }
    
    [self.lineChartV setTitles:model.dates WithGap:gap left:left];
    self.noDataTipsL.hidden = !model.isNodata;
    switch (model.type) {
        case SummaryData_Mark:
            self.lineChartV.lineColor = COLOR_HEXSTRING(@"#46a8ef");
            break;
        case SummaryData_OilPrice:
            self.lineChartV.lineColor = COLOR_HEXSTRING(@"#ffb400");
            break;
        case SummaryData_Mile:
            self.lineChartV.lineColor = COLOR_HEXSTRING(@"#37bdd6");
            break;
        case SummaryData_AvgOil:
            self.lineChartV.lineColor = COLOR_HEXSTRING(@"#fd6500");
            break;
        default:
            self.lineChartV.lineColor = COLOR_HEXSTRING(@"#454545");
            break;
    }
    [self.lineChartV strokeChart];
}


@end